package com.liwq.base.juc;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author : liwq
 * @version : 1.0
 * @date : 2022/7/20
 **/
public class ReentrantLockTest {

    public static void main(String[] args) throws InterruptedException {

        //创建锁
        Lock lock = new ReentrantLock();
        String threadName = Thread.currentThread().getName() + "线程";
        for (int i = 1; i < 4; i++) {
            Thread.sleep(1000);
            //获取锁
            lock.lock();
            System.out.println(threadName + "第" + i + "次获取到锁 ");
        }

        for (int i = 1; i < 4; i++) {
            try {
                Thread.sleep(1000);
            } finally {
                //释放锁
                lock.unlock();
                ;
                System.out.println(threadName + "第" + i + "次释放锁 ");
            }
        }

    }
}


